package com.sc.study.exam.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.sc.study.exam.service.IScTestexamService;
import com.sc.study.student.service.IScStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.sc.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.sc.study.exam.domain.ScTestexam;
import com.sc.study.exam.mapper.ScStudentTestexamMapper;
import com.sc.study.exam.domain.ScStudentTestexam;
import com.sc.study.exam.service.IScStudentTestexamService;
import org.springframework.util.Assert;

/**
 * 学生试卷Service业务层处理
 * 
 * @author sc
 * @date 2022-03-16
 */
@Service
public class ScStudentTestexamServiceImpl implements IScStudentTestexamService 
{
    @Autowired
    private ScStudentTestexamMapper scStudentTestexamMapper;

    @Autowired
    private IScStudentService studentService;

    @Autowired
    private IScTestexamService testexamService;


    /**
     * 查询学生试卷列表
     * 带出学生和 试卷
     * @param scStudentTestexam 学生试卷
     * @return 学生试卷
     */
    @Override
    public List<ScStudentTestexam> selectScStudentTestexamByStudentAndTextExam(ScStudentTestexam scStudentTestexam)
    {
        List<ScStudentTestexam> scStudentTestexams = scStudentTestexamMapper.selectScStudentTestexamList(scStudentTestexam);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        //获取当前时间
        Date currentTime = new Date();
        //带出学生和试卷
        scStudentTestexams.stream().forEach(item -> {

            //设置是否显示删除按钮
            try {
                Date startTime = simpleDateFormat.parse(item.getStarttime());
                Date endTime = simpleDateFormat.parse(item.getEndtime());

                if(currentTime.after(startTime) && currentTime.before(endTime)) item.setShowRemoveButton(false);

            } catch (ParseException e) {
                e.printStackTrace();
            }

            item.setStudent(studentService.selectScStudentById(item.getStudentid()));
            item.setTestexam(testexamService.selectScTestexamById(item.getTestexamid()));
        });

        return scStudentTestexams;
    }



    /**
     * 查询学生试卷
     * 
     * @param id 学生试卷主键
     * @return 学生试卷
     */
    @Override
    public ScStudentTestexam selectScStudentTestexamById(Long id)
    {
        return scStudentTestexamMapper.selectScStudentTestexamById(id);
    }

    /**
     * 查询学生试卷列表
     * 
     * @param scStudentTestexam 学生试卷
     * @return 学生试卷
     */
    @Override
    public List<ScStudentTestexam> selectScStudentTestexamList(ScStudentTestexam scStudentTestexam)
    {
        return scStudentTestexamMapper.selectScStudentTestexamList(scStudentTestexam);
    }

    /**
     * 新增学生试卷
     * 
     * @param scStudentTestexam 学生试卷
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScStudentTestexam(ScStudentTestexam scStudentTestexam)
    {
        int rows = scStudentTestexamMapper.insertScStudentTestexam(scStudentTestexam);
        insertScTestexam(scStudentTestexam);
        return rows;
    }

    /**
     * 修改学生试卷
     * 
     * @param scStudentTestexam 学生试卷
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScStudentTestexam(ScStudentTestexam scStudentTestexam)
    {
        scStudentTestexamMapper.deleteScTestexamById(scStudentTestexam.getId());
        insertScTestexam(scStudentTestexam);
        return scStudentTestexamMapper.updateScStudentTestexam(scStudentTestexam);
    }

    /**
     * 批量删除学生试卷
     * 
     * @param ids 需要删除的学生试卷主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScStudentTestexamByIds(Long[] ids)
    {
        scStudentTestexamMapper.deleteScTestexamByIds(ids);
        return scStudentTestexamMapper.deleteScStudentTestexamByIds(ids);
    }

    /**
     * 删除学生试卷信息
     * 
     * @param id 学生试卷主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScStudentTestexamById(Long id)
    {
        scStudentTestexamMapper.deleteScTestexamById(id);
        return scStudentTestexamMapper.deleteScStudentTestexamById(id);
    }

    /**
     * 根据考试编号和学生进行删除
     * @param testExamId 考试编号
     * @param studentIds 学生编号集合
     * @return
     */
    @Transactional
    @Override
    public int removeByTestExamIdAndStudentId(Integer testExamId, Integer[] studentIds) {
        return scStudentTestexamMapper.removeByTestExamIdAndStudentId(testExamId, studentIds);
    }

    @Override
    @Transactional
    public int batchAdd(List<ScStudentTestexam> scStudentTestExamList) {
        try{
            scStudentTestExamList.forEach(item -> scStudentTestexamMapper.insertScStudentTestexam(item));
            return 1;
        }catch (Exception e){
            throw new RuntimeException("批量添加失败");
        }

    }

    /**
     * 批量添加 scTestExam 里面必须携带学生id集合
     * @param scTestExam 里面必须携带学生id集合
     * @return
     */
    @Override
    public int batchAddByStudentIds(ScTestexam scTestExam) {
        return scStudentTestexamMapper.batchAddByStudentIds(scTestExam);
    }

    /**
     * 导入excel: 批量添加
     * @param importTestExamId
     * @param studentTestExamList
     * @return
     */
    @Override
    public int excelImportBatchAdd(Long importTestExamId, List<ScStudentTestexam> studentTestExamList) {
        ScTestexam scTestexam = testexamService.selectScTestexamById(importTestExamId);

        Assert.notNull(scTestexam, "当前选择的考试不存在");

        return scStudentTestexamMapper.excelImportBatchAdd(scTestexam, studentTestExamList);
    }

    @Override
    public Long getIdByStudentAndTextExam(Long studentId, Long testExamId) {
        return scStudentTestexamMapper.getIdByStudentAndTextExam(studentId, testExamId);
    }

    /**
     * 新增考试信息
     * 
     * @param scStudentTestexam 学生试卷对象
     */
    @Transactional
    public void insertScTestexam(ScStudentTestexam scStudentTestexam)
    {
        List<ScTestexam> scTestexamList = scStudentTestexam.getScTestexamList();
        Long id = scStudentTestexam.getId();
        if (StringUtils.isNotNull(scTestexamList))
        {
            List<ScTestexam> list = new ArrayList<ScTestexam>();
            for (ScTestexam scTestexam : scTestexamList)
            {
                scTestexam.setId(id);
                list.add(scTestexam);
            }
            if (list.size() > 0)
            {
                scStudentTestexamMapper.batchScTestexam(list);
            }
        }
    }
}
